<?php

class Front_IndexController extends Kieumau_Controller_Frontend
{
	//private $prime = array(2, 3, 5, 7, 11);//25 trường hợp
    public function init()
    {
		parent::init();
		try{
	    	$dirTemplate = TEMPLATE_PATH;
	    	$this->createTemplate($dirTemplate );
	    	
	    	
	    	
	    	
	    	/*
	    	//Change Language
	    	$translate = new Zend_Translate('gettext',
	    			APPLICATION_PATH . "/languages/",
	    			null,
	    			array('scan' => Zend_Translate::LOCALE_DIRECTORY));
	    	$registry = Zend_Registry::getInstance();
	    	$registry->set('Zend_Translate', $translate);
	    	
	    	$translate->setLocale((string)$this->defaultLanguage);
	    	
	    	/**/
	    	
    	}catch(Exception $ex){
    		echo "error: ".$ex->getMessage();
    		die;
    	}
    }
	
	public function preDispatch() {
		/*
		$auth = Zend_Auth::getInstance ();
		if (! $auth->hasIdentity ()) {
			$actionAllow = array('index', 'login','signup','active','message', 'search','searchresult');//Cho phép vào mà ko cần đăng nhập
			if (! in_array($this->_request->getActionName (), $actionAllow)) {
				//Lưu url bị block vào session blockedUrl
				$blockedUrl = new Zend_Session_Namespace ( 'blockedUrl' );
				$blockedUrl->blockedUrl = $this->getCurrentPageURL();
				
				$this->_redirect ( '/index' );//Đưa sang trang index
			}
		}
		/**/
		
	}

    public function createTemplate($dirTemplate, $sectionConfig = 'template', $fileConfig = 'default/template.ini' ){
    	$module =$this->getRequest()->getModuleName();
    	
    	$filename = $dirTemplate . '/'.$module.'/' . $fileConfig;
    
    	$section = $sectionConfig;
    	$config = new Zend_Config_Ini($filename, $section);
    	$config = $config->toArray();
    
    	$baseUrl = $this->_request->getBaseUrl();
    	
    	$dirTemplateUrl = $baseUrl . $config['url'];
    	$dirJs = $dirTemplateUrl . $config['dirJs'];
    	
    	
    	$this->view->dirCss = $dirTemplateUrl . $config['dirCss'];
    	$this->view->dirImg = $dirTemplateUrl . $config['dirImg'];
    	$this->view->dirJs = $dirTemplateUrl . $config['dirJs'];
		
    }
	
	public function indexAction(){
		/*============== Debug Part 1 =========================================
		echo "<br/> Controller name: ".$this->getRequest()->getControllername();
		echo "<br/> Module name: ".$this->getRequest()->getModulename();
		echo "<br/> Action name: ".$this->getRequest()->getActionname();
		echo "<br/>index action";
		/**/
	}
	
	public function newsAction(){
		$newsTable = new Front_Model_DbTable_News();
		
		//Lấy danh sách tin tức ra
		$newsList = $newsTable->getNewsOnline('news_language','status = ?', array(1));
		if(!empty($newsList))
			$this->view->newsList = $newsList;
	}
	
	public function newsdetailAction(){
		$id = $this->getRequest()->getParam('id',0);
		$id = (int)$id;
		if($id > 0){
			$newsTable = new Front_Model_DbTable_News();
			
			$this->view->newsDetail = $newsTable->getNewsDetail($id);
		}
		else{
			echo 'This news does not exists';
			
		}
	}
	
	public function courseAction(){
		//Get all data from course
		$taxonomyTable 	= new Front_Model_DbTable_Taxonomy();
		$categoryTable 	= new Front_Model_DbTable_Category();
		$pageCategoryTable = new Front_Model_DbTable_PageCategory();
		$pageTable		= new Front_Model_DbTable_Page();
		
		$taxonomyList = $taxonomyTable->getAllByTable('enabled = 1');
		
		
		//Get Category inside this course
		foreach($taxonomyList as &$oneTaxonomy){
			//Get list of category inside this taxonomy
			$oneTaxonomy['category'] = $categoryTable->getAllByTable('id in ( SELECT category_id FROM category_taxonomy WHERE taxonomy_id = '.$oneTaxonomy['id'].') AND enabled = 1' );
			$oneTaxonomy['lastPost'] = array();
			
			//Init the number of page
			$numberOfPost = 0;
			
			if(!empty($oneTaxonomy['category'])){
				
				foreach($oneTaxonomy['category'] as &$oneCategory){
					//Get list of page inside this category
					$oneCategory['page'] = $pageCategoryTable->getPageChildren($oneCategory['id'], $this->defaultLanguageId);
					
					//Init last post of this category
					$oneCategory['lastPost'] = array();
					
					//Set the anchor date
					$last_post_date = date('Y-m-d',strtotime('-100years'));
					
					//Find the last post of this category
					foreach($oneCategory['page'] as &$onePage){
						//Find the lastest modify
						if($last_post_date < $onePage['last_modify']){
							$last_post_date = $onePage['last_modify'];
							$oneCategory['lastPost'] = $onePage;
						}
					}
					
				}//END foreach as $oneCategory
				
				
				//get the last post of this taxonomy
				$last_post_date = date('Y-m-d',strtotime('-100years'));
				foreach($oneTaxonomy['category'] as &$oneCategory){
					
					if(!empty($oneCategory['lastPost'])){
						if($last_post_date  < $oneCategory['lastPost']['last_modify']){
							$oneTaxonomy['lastPost'] = $oneCategory['lastPost'];
						}
					}
					
					//Count the quantity of page
					$numberOfPost += count($oneCategory['page']);
				}
				
			}
			
			$oneTaxonomy['numberOfPost'] = $numberOfPost;
		}
		
		//Assign to view
		$this->view->taxonomyList = $taxonomyList;
	}

}